/*
 * Copyright (c) 2022 HPMicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include <stdio.h>
#include "board.h"
#include "hpm_ppor_drv.h"
#include "hpm_l1c_drv.h"
#include "hpm_gpio_drv.h"
#include "hpm_gpiom_drv.h"
#include "hpm_debug_console.h"
#include "usb_config.h"

#include "bootuf2.h"

#include "jtag.h"
#include "riscv_debug.h"
#include "target.h"
#include "flash/flash.h"

extern target_t target_riscv;

extern void msc_bootuf2_init(uint8_t busid, uintptr_t reg_base);

int main(void)
{
    board_init();

    board_init_usb((USB_Type *)CONFIG_HPM_USBD_BASE);
    intc_set_irq_priority(CONFIG_HPM_USBD_IRQn, 2);

    printf("HPM Firmware Loader. \n");

    msc_bootuf2_init(0, CONFIG_HPM_USBD_BASE);
    while (1)
    {
        extern uint32_t done_flag;
        extern uint32_t done_cnt;

        board_delay_ms(1000);

        if (done_flag)
        {
            if (done_cnt)
            {
                done_cnt--;
            }
            else
            {
                int ret = bootuf2_is_write_done();
                done_flag = 0;
                // target_riscv.reset(0);
                // printf("ret = %d ppor_sw_reset()\r\n", ret);
                // ppor_sw_reset(HPM_PPOR, 10);
            }
        }
    }

    return 0;
}
